package com.songtech.common.dictionary.service.impl;

import com.songtech.common.dictionary.bean.SysDictionary;
import com.songtech.common.dictionary.bean.SysDictionaryGroup;
import com.songtech.common.dictionary.bean.ThreeSelect;
import com.songtech.common.dictionary.dao.SysDictionaryGroupMapper;
import com.songtech.common.dictionary.dao.SysDictionaryMapper;
import com.songtech.common.dictionary.service.SysDictionaryGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: RENTIE
 * Date: 2018-06-19 10:11
 */
@Service
public class SysDictionaryGroupServiceImpl implements SysDictionaryGroupService {

    @Resource
    private SysDictionaryGroupMapper sysDictionaryGroupMapper;

    @Resource
    private SysDictionaryMapper sysDictionaryMapper;

    @Override
    public List<SysDictionaryGroup> selectVenderByCompanyId(String companyId) {
        return sysDictionaryGroupMapper.selectVenderByCompanyId(companyId);
    }

    @Override
    public List<ThreeSelect> selectThreeSelect( List<SysDictionaryGroup> groups) throws Exception {
       List<ThreeSelect> returnValues=new ArrayList<ThreeSelect>();
       if (groups!=null){
           groups.forEach(g->{
               ThreeSelect first = new ThreeSelect(g.getDictgroupId(),g.getDictgroupName());
               //产品
               List<SysDictionary> products = sysDictionaryMapper.selectProduct(g.getDictgroupId());
                if (products!=null){
                    List<ThreeSelect> secondChilds=new ArrayList<ThreeSelect>();
                    products.forEach(p->{
                        ThreeSelect second = new ThreeSelect(p.getDictId(),p.getDictName());
                        //型号
                        List<SysDictionary> versions = sysDictionaryMapper.selectVersion(p.getDictId());
                        if (versions!=null){
                            List<ThreeSelect> thirdChilds=new ArrayList<ThreeSelect>();
                            versions.forEach(v->{
                                ThreeSelect third = new ThreeSelect(v.getDictId(),v.getDictName());
                                thirdChilds.add(third);
                            });
                            second.setChilds(thirdChilds);
                        }
                        secondChilds.add(second);
                    });
                    first.setChilds(secondChilds);

                }
               returnValues.add(first);
           });
       }
        return returnValues;
    }

    @Override
    public List<SysDictionaryGroup> selectVenderByCode(String code) throws Exception {
        return sysDictionaryGroupMapper.selectVenderByCode(code);
    }


}
